package net.zjitc.dao.impl;

import net.zjitc.dao.UserDao;
import net.zjitc.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/2/27.
 */
@Repository

public class UserDaoImpl implements UserDao{
    @Resource
    private SessionFactory sessionFactory;
    @Override
    public User findByUsernamePassword(String username, String password) {
        /*初始化几个数据，从里面找登录的用户*/
       /*this.init();*/


//        openSession();区别
        Session session = sessionFactory.getCurrentSession();
//                Transaction transaction = session.beginTransaction();
                User user = (User) session.createQuery(
                        "from User u where u.username = ? and u.password = ?")
                        .setParameter(0,username)
                        .setParameter(1,password)
                        .uniqueResult();
//                transaction.commit();
//                session.close();
        return user;
    }
}
